<?php
class Mysql
{
	private static $host = "localhost";
	private static $user = "root";
	private static $password = "root";
	private static $dbName = "student";           //数据库名
	private static $charset = "utf8";          //字符编码
	private static $port = "3306";            //端口号
	public $conn = null;
	function __construct()
	{
		$this->conn = new mysqli(self::$host, self::$user, self::$password, self::$dbName, self::$port);
		if (!$this->conn) {
			die("数据库连接失败！" . $this->conn->connect_error);
		} else {
			$this->conn->query("set names " . self::$charset);
		}
	}


	function QueryReturnId($sql)
	{
		$this->conn->query($sql);
		return $this->conn->insert_id;
	}
	function Query($sql)
	{
		$res = $this->conn->query($sql);
		return $res;
	}

	//执行sql语句
	function sql($sql)
	{
		$res = $this->conn->query($sql);
		if (!$res) {
			echo "数据操作失败";
		} else {
			if ($this->conn->affected_rows > 0) {
				return $res;
			} else {
				echo "0行数据受影响！";
			}
		}
	}
	//返回受影响数据行数
	function getResultNum($sql)
	{
		$res = $this->conn->query($sql);
		if ($res) {
			return mysqli_num_rows($res);
		} else {
			return 0;
		}
	}
	/*查询表格参数一表明   参数二 查询语句 $_tabl,$sea 已经使用*/
	public function selectTbale($_tabl, $sea)
	{
		if ($sea == "") {
			//此处里面已经全部使用  根据表格进行全部查询
			$sql = "select * from " . $_tabl . " order by id desc";
			$result = $this->conn->query($sql);
			if ($result) {
				while ($row = mysqli_fetch_assoc($result)) {
					$arr[] = $row;
				}
				return  json_encode($arr);
			} else {
			}
		} else {
			if ($_tabl == "resource") {
				$sql = "select * from " . $_tabl . " where companyName like '%{$sea}%' order by id";
			} else if ($_tabl == "prointroduce") {
				$sql = "select * from " . $_tabl . " where name like '%{$sea}%' order by id";
			} else if ($_tabl == "resourcedetails") {
			} else if ($_tabl == "output") {
				$sql = "select * from " . $_tabl . " where peopleName like '%{$sea}%' order by id desc";
			}
			$result = $this->conn->query($sql);
			while ($row = mysqli_fetch_assoc($result)) {
				$arr[] = $row;
			}
			if ($_tabl == "output") {
				return json_encode($arr);
			} else {
				return $arr;
			}
		}
	}
	/*根据id 查询表格（查询表格参数一表明   参数二 查询id的号码）已经使用*/
	public function idTbale($_tabl, $id)
	{
		$sql = "select * from " . $_tabl . " where id ='" . $id . "'";
		$result = $this->conn->query($sql);
		while ($row = mysqli_fetch_assoc($result)) {
			$arr[] = $row;
		}
		return  json_encode($arr);
	}
	/* 根据语句进行查询   例如like语句  传入一个sql语句  即可返回一个列表 已经使用*/
	public function likeSelect($sql)
	{
		$result = $this->conn->query($sql);
		while ($row = mysqli_fetch_assoc($result)) {
			$arr[] = $row;
		}
		return  json_encode($arr);
	}
	/* 根据语句进行查询   例如like语句  传入一个sql语句  即可返回一个列表 和上面的区别是一个是layui 的查询表格 已经使用*/
	public function likeSelectLayui($sql)
	{
		$result = $this->conn->query($sql);
		if ($result) {
			while ($row = mysqli_fetch_assoc($result)) {
				$arr[] = $row;
			}
			return $arr;
		} else {
			return 0;
		}
	}
	/*查询表格参数一表明 主要给layui 的查询表格使用    已经使用*/
	public function selectTbaleLlay($_tabl)
	{
		//此处里面已经全部使用  根据表格进行全部查询
		$sql = "select * from " . $_tabl . " order by id ";
		$result = $this->conn->query($sql);
		while ($row = mysqli_fetch_assoc($result)) {
			$arr[] = $row;
		}
		return  $arr;
	}
	/*根据id 删除表格内容（查询表格参数一表明   参数二 删除id的号码）已经使用*/
	public function deleteTbale($tabl, $id)
	{
		$sql = "delete from " . $tabl . " where id ='" . $id . "'";
		return $this->Query($sql);
	}
	/*登录验证  已经使用*/
	public function loginPhp($username, $password)
	{
		$arrays = array(array('one' => 'nameerror', 'two' => 'passerror'));
		$sql = "select count(*) from teachers where username= '" . $username . "'";
		$sqlname = $this->conn->query($sql);

		$row = @mysqli_fetch_row($sqlname);
		$num = $row[0];

		//查看用户是否存在
		if (!$num) {
			return json_encode($arrays[0]['one']); //不存在返回nameerror
		} else {
			$sqlp = "select userpwd from teachers where username='" . $username . "'";
			$sqlpass = $this->conn->query($sqlp);
			$passarray = @mysqli_fetch_row($sqlpass); //获得一个只有一行数据的数族
			$passval = $passarray[0]; //这里才是对应用户的密码
			//判断该用户的密码是否正确
			if ($passval != $password) {
				return json_encode($arrays[0]['two']); //不正确返回passerror
			} else {
				$sqlo = "select * from teachers t inner join company c on t.company_id = c.id where t.username= '" . $username . "'";
				$sqlopa = $this->conn->query($sqlo);
				$row1 = @mysqli_fetch_row($sqlopa);
				return json_encode($row1); //登录成功
			}
		}
	}
	/*登录验证  已经使用*/
	public function loginClass($username, $password)
	{
		$arrays = array(array('one' => 'nameerror', 'two' => 'passerror'));
		$sql = "select count(*) from class where name= '" . $username . "'";
		$sqlname = $this->conn->query($sql);

		$row = @mysqli_fetch_row($sqlname);
		$num = $row[0];

		//查看用户是否存在
		if (!$num) {
			return json_encode($arrays[0]['one']); //不存在返回nameerror
		} else {
			$sqlp = "select password from class where name='" . $username . "'";
			$sqlpass = $this->conn->query($sqlp);
			$passarray = @mysqli_fetch_row($sqlpass); //获得一个只有一行数据的数族
			$passval = $passarray[0]; //这里才是对应用户的密码
			//判断该用户的密码是否正确
			if ($passval != $password) {
				return json_encode($arrays[0]['two']); //不正确返回passerror
			} else {
				$sqlo = "select * from class where name= '" . $username . "'";
				$sqlopa = $this->conn->query($sqlo);
				$row1 = @mysqli_fetch_row($sqlopa);
				return json_encode($row1); //登录成功
			}
		}
	}
	//关闭数据库 已经使用
	public function close()
	{
		@mysqli_close($this->conn);
	}
}
